/*********************************************************************************************************
* Trigger Name: NMRSLRProcessingTrig 
* Author: Accenture IDC
* Date: 3rd March, 2012  
* Requirement/Project Name: NMR 
* Description: Trigger invoked on before Update of SLR.
*********************************************************************************************************/
trigger NMRSLRProcessingTrig on Standardized_Lead_Request__c (before update) {

    Map<Id, Standardized_Lead_Request__c> slrListMap = new Map<Id, Standardized_Lead_Request__c>();
    Map<string,Standardized_Lead_Request__c> slrRecordMap = new Map<string,Standardized_Lead_Request__c>();
    List<Standardized_Lead_Request__c> slrList = new List<Standardized_Lead_Request__c>();
    Set<string> phoneSet = new Set<string>();
    //retrieving the SLR record id
    ID slrRecordTypeID = RecordTypeHelper.GetRecordTypeID('Standardized_Lead_Request__c', 'Standardized Lead Request');
     
    for(Standardized_Lead_Request__c slr : trigger.new)
    {
        if (slrRecordTypeID == slr.RecordTypeId && slr.Status__c != trigger.oldMap.get(slr.Id).Status__c)
        {//if Status field has been updated
            if('SIL Process Completed'.equalsIgnoreCase(slr.Status__c) && slr.CWS_Failure__c)
                slr.Status__c = 'SFDC Invalid'; // set as invalid as CWS call failed
            else if ('SIL Process Completed'.equalsIgnoreCase(slr.Status__c))
                slrListMap.put(slr.Id, slr); //add to map for standardization
                          
        }
    }
    
    if(slrListMap.size() > 0){
        //Send to NMRSLRProcessing class for standardization
        NMRSLRProcessing.slrStandardization(slrListMap);
        for(Standardized_Lead_Request__c newSLR : slrListMap.Values()){
            if(newSLR.Status__c.equalsIgnoreCase('SLR Enrichment Completed')){
                //this SLR should go for Lead Enrichment  
                slrList.add(newSLR);
                slrRecordMap.put(NMRUtil.formatPhone(newSLR.Business_Phone__c), newSLR);
                phoneSet.add(NMRUtil.formatPhone(newSLR.Business_Phone__c));
            }
        }
    
        if(slrList.size()>0)//Invoking NMRMasterLeadEnrichment for Lead enrichment
            NMRMasterLeadEnrichment.findLeadtoEnrich(slrRecordMap, phoneSet);
    }        
  
}